
/**
     * 获取本周、本季度、本月、上月的开端日期、停止日期
     */
var now = new Date() // 当前日期
var nowDayOfWeek = now.getDay() // 今天本周的第几天
var nowDay = now.getDate() // 当前日
var nowMonth = now.getMonth() // 当前月
var nowYear = now.getYear() // 当前年
nowYear += (nowYear < 2000) ? 1900 : 0 //

var lastMonthDate = new Date() // 上月日期
lastMonthDate.setDate(1)
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
// var lastYear = lastMonthDate.getYear()
// var lastMonth = lastMonthDate.getMonth()

// 获得某月的天数
function getMonthDays(myMonth) {
  var monthStartDate = new Date(nowYear, myMonth, 1)
  var monthEndDate = new Date(nowYear, myMonth + 1, 1)
  var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
  return days
}

// 获得本季度的开端月份
function getQuarterStartMonth() {
  var quarterStartMonth = 0
  if (nowMonth < 3) {
    quarterStartMonth = 0
  }
  if (nowMonth > 2 && nowMonth < 6) {
    quarterStartMonth = 3
  }
  if (nowMonth > 5 && nowMonth < 9) {
    quarterStartMonth = 6
  }
  if (nowMonth > 8) {
    quarterStartMonth = 9
  }
  return quarterStartMonth
}

// 格局化日期：yyyy-MM-dd
const formatDate = function(date) {
  var myyear = date.getFullYear()
  var mymonth = date.getMonth() + 1
  var myweekday = date.getDate()

  if (mymonth < 10) {
    mymonth = '0' + mymonth
  }
  if (myweekday < 10) {
    myweekday = '0' + myweekday
  }
  return (myyear + '-' + mymonth + '-' + myweekday)
}
// 获得本周的开端日期
const getWeekStartDate = function() {
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
  return formatDate(weekStartDate)
}

// 获得本周的停止日期
const getWeekEndDate = function() {
  var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek))
  return formatDate(weekEndDate)
}

// 获得本月的开端日期
const getMonthStartDate = function() {
  var monthStartDate = new Date(nowYear, nowMonth, 1)
  return formatDate(monthStartDate)
}

// 获得本月的停止日期
const getMonthEndDate = function() {
  var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth))
  return formatDate(monthEndDate)
}

// 获得本季度的开端日期
const getQuarterStartDate = function() {
  var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1)
  return formatDate(quarterStartDate)
}

// 或的本季度的停止日期
const getQuarterEndDate = function() {
  var quarterEndMonth = getQuarterStartMonth() + 2
  var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth))
  return formatDate(quarterStartDate)
}

// 获得本年开始日期
const getYearStartDate = function() {
  var yearStartDate = new Date(nowYear, 0, 1)
  return formatDate(yearStartDate)
}
// 获得本年结束日期
const getYearEndDate = function getYearEndDate() {
  var yearEndDate = new Date(nowYear, 11, 30)
  return formatDate(yearEndDate)
}

// 计算指定日期的星期
const getAssignDateWeek = function(date) {
  var _date = new Date(date)
  // getDay() 返回表示星期的某一天
  var num = _date.getDay(_date)
  var week
  switch (num) {
    case 0:
      week = '周日'
      break
    case 1:
      week = '周一'
      break
    case 2:
      week = '周二'
      break
    case 3:
      week = '周三'
      break
    case 4:
      week = '周四'
      break
    case 5:
      week = '周五'
      break
    case 6:
      week = '周六'
      break
    default:
      break
  }
  return week
}

// 标准时间格式截取 mm-dd
const getDateTimeDate = function(dateTime) {
  var _date = new Date(dateTime)
  // getDay() 返回表示星期的某一天
  var month = _date.getMonth(_date) + 1
  var day = _date.getDate(_date)
  return month + '-' + day
}

// 标准时间格式截取 yy-mm
const getYearMonth = function(dateTime) {
  var _date = new Date(dateTime)
  var year = _date.getFullYear()
  var month = _date.getMonth(_date) + 1

  return year + '-' + month
}

function doHandleMonth(month) {
  var m = month
  if (month.toString().length === 1) {
    m = '0' + month
  }
  return m
}
// js获取前后n天的时间
const getDay = function(day) {
  var today = new Date()

  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day

  today.setTime(targetday_milliseconds)

  var tYear = today.getFullYear()
  var tMonth = today.getMonth()
  var tDate = today.getDate()
  tMonth = doHandleMonth(tMonth + 1)
  tDate = doHandleMonth(tDate)
  return tYear + '-' + tMonth + '-' + tDate
}

export default {
  formatDate: formatDate,
  getWeekStartDate: getWeekStartDate,
  getWeekEndDate: getWeekEndDate,
  getMonthStartDate: getMonthStartDate,
  getMonthEndDate: getMonthEndDate,
  getQuarterStartDate: getQuarterStartDate,
  getQuarterEndDate: getQuarterEndDate,
  getYearStartDate: getYearStartDate,
  getYearEndDate: getYearEndDate,
  getAssignDateWeek: getAssignDateWeek,
  getDateTimeDate: getDateTimeDate,
  nowYear: nowYear,
  nowMonth: nowMonth,
  nowDay: nowDay,
  getDay: getDay,
  getYearMonth: getYearMonth
}

